//microcommand define factorize
function supNum(n) {
  return Array.from(String(0 | n), d => "⁰¹²³⁴⁵⁶⁷⁸⁹"[d]).join("")
}

signature("<...number>", { strictOptions: true })
alias("分解质因数")
action(async ({ session }, ...nums) => {
  if (!nums.length) return session.text("internal.insufficient-arguments")
  function s(n) {
    let nn = 0n, k = 3n, arr = []
    if (n > 10n ** 18n) return session.text(".too-big")
    nn = n
    while ((nn & 1n) === 0n) {
      arr.push(2n)
      nn = nn >> 1n
    }
    while (nn > 1n) {
      if (nn % k === 0n) {
        arr.push(k)
        nn = nn / k
      } else {
        k = k + 2n
        if (k * k > nn) {
          arr.push(nn)
          break
        }
      }
    }
    if (arr.length == 1) return session.text(".prime", [String(arr)])
    const powers = new Map()
    for (const prime of arr) {
      if (powers.has(prime)) {
        powers.set(prime, powers.get(prime) + 1)
      } else {
        powers.set(prime, 1)
      }
    }
    const r = Array.from(
      powers,
      ([prime, power]) => `${prime}${power > 1 ? supNum(power) : ""}`
    ).join(" × ")
    return `${n} = ${r}`
  }
  try {
    nums = nums.map(BigInt)
    if (nums.some(n => n <= 1n)) return session.text(".expect-greater-than-one")
  } catch {
    return session.text(".invalid-input")
  }
  return nums.map(s).join("<br/>")
})
locale("zh-CN", {
  description: "分解质因数",
  usage: "输入格式：一个或多个（空格分隔）大于 1、不大于 10¹⁸ 的十进制整数",
  messages: {
    "invalid-input": "输入无效…",
    "expect-greater-than-one": "输入应为大于 1 的整数",
    "too-big": "数字过大…",
    prime: "{0} 是质数",
  },
})